<?php
/**
 * 作用：公用模块，用于放置零碎，不能组成模块的功能函数及常量
 * 作者: shijy@soe-soe.com
 * 日期: 2016/5/11 16:09
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */


namespace Common\Model;


class OrderModel extends BaseModel{

    const ORDER_STATUS_UNPAY    =1;//待支付
    const ORDER_STATUS_PAID     =2;//已支付
    const ORDER_STATUS_CANCEL   =3;//取消
    const ORDER_STATUS_COMPETE  =4;//完成

    public static function getStatusKv(){
        return array(
            self::ORDER_STATUS_UNPAY =>'待支付',
            self::ORDER_STATUS_PAID =>'已支付',
            self::ORDER_STATUS_CANCEL =>'取消',
            self::ORDER_STATUS_COMPETE =>'完成',
        );
    }

    const REFUND_STATUS_APPLY   =1;//申请中
    const REFUND_STATUS_SUCCESS =2;//退款成功
    const REFUND_STATUS_FAIL    =3;//退款失败


    private static $orderInstance=null;
    public static function getOrderInstance(){
        if(!self::$orderInstance){
            self::$orderInstance=M('o_order',null,DB_MAIN_CFG);
        }
        return self::$orderInstance;
    }


    /**
     * 返回指定日期房价
     * @param $rid
     * @param $dateStr
     * @return array
     */
    public  static function calcRoomFee($rid,$dateStr)
    {
        $dateArr = explode(',', $dateStr);
        foreach ($dateArr as $date){
            if(strtotime($date)===false){
                return array('status' => 1, 'msg' => "日期格式不正确");
            }
        }



        $dateStr = implode("','", $dateArr);


        $roomStatus = M('r_status', null, DB_MAIN_CFG)->where('rid=' . $rid . " and date in ('{$dateStr}')  ")->getField('date,status', true);
        $errDate = array();
        foreach ($roomStatus as $date => $status) {
            if ($status != RoomModel::ROOM_STATUS_IDLE) {
                $errDate[] = $date;
            }
        }
        if (count($errDate) > 0){
            return array('status' => 1, 'msg' => implode(',', $errDate) . "等日期不可预订");
        }

        $priceSet       = M('r_price_common',null,DB_MAIN_CFG)->where('rid='.$rid)->find();

        $list=array();
        foreach($dateArr as $tmpDate){


            $w=date('w',strtotime($tmpDate));
            switch ($w){
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                    $isWorkDay=true;
                    break;
                case 5:
                case 6:
                    $isWorkDay=false;
                    break;
            }

                if($priceSet['devide'] !='0000-00-00' && $tmpDate >= $priceSet['devide']){
                    if ($isWorkDay){
                        $v=$priceSet['workDayPrice2'];
                    }else{
                        $v=$priceSet['weekEndPrice2'];
                    }
                }else{
                    if ($isWorkDay){
                        $v=$priceSet['workDayPrice'];
                    }else{
                        $v=$priceSet['weekEndPrice'];
                    }
                }
                $list[$tmpDate] = $v;
        }

        $specialPrice   = M('r_price_special',null,DB_MAIN_CFG)->where('rid='.$rid." and date in ('{$dateStr}') ")->field('date,price')->select();
        foreach ( $specialPrice as $special){
            $list[$special['date']]=$special['price'];
        }

        return array('status'=>0,'fee'=>array_sum($list),'detail'=>$list);


    }






    private static $refundinstance=null;
    public static function getRefundInstance(){
        if(!self::$refundinstance){
            self::$refundinstance=M('o_refund',null,DB_MAIN_CFG);
        }
        return self::$refundinstance;
    }

}